﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using EmbeddedSensorCloud.HTTP;
using EmbeddedSensorCloud.Plugin.MicroERP;
using Newtonsoft.Json;

namespace EmbeddedSensorCloud.UnitTest.MicroErpTest
{
    [TestClass]
    public class Commands_Test
    {
        [TestMethod]
        [Description("Tests if the CreateCustomerCommand checks the request body correctly")]
        [ExpectedException(typeof(BadRequestException))]
        public void CreateCustomerCommand_EmtpyBody_Test()
        {
            CreateCustomerCommand command = new CreateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/customer", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the CreateCustomerCommand recognizes a wrong request method")]
        public void CreateCustomerCommand_WrongMethod_Test()
        {
            CreateCustomerCommand command = new CreateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/customer", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the CreateCustomerCommand recognizes a wrong request Url")]
        public void CreateCustomerCommand_WrongUrl_Test()
        {
            CreateCustomerCommand command = new CreateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/customer/5", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the CreateCustomerCommand checks the request method and Url correctly")]
        public void CreateCustomerCommand_RightMethodAndUrl_Test()
        {
            CreateCustomerCommand command = new CreateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/customer/", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsTrue(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the DeleteCustomerCommand recognizes a request Url with to many parts")]
        [ExpectedException(typeof(BadRequestException))]
        public void DeleteCustomerCommand_ToManyUrlParts_Test()
        {
            DeleteCustomerCommand command = new DeleteCustomerCommand(new MySQLCustomerDAO(""));

            Request testRequest = new Request("DELETE", "/api/customer/5/1", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the DeleteCustomerCommand recognizes a request Url with a non-numeric id in it")]
        [ExpectedException(typeof(BadRequestException))]
        public void DeleteCustomerCommand_NonNumbericId_Test()
        {
            DeleteCustomerCommand command = new DeleteCustomerCommand(new MySQLCustomerDAO(""));

            Request testRequest = new Request("DELETE", "/api/customer/a", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the DeleteCustomerCommand recognizes a wrong request method")]
        public void DeleteCustomerCommand_WrongMethod_Test()
        {
            DeleteCustomerCommand command = new DeleteCustomerCommand(new MySQLCustomerDAO(""));

            Request testRequest = new Request("PUT", "/api/customer/5", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the DeleteCustomerCommand recognizes a wrong request Url")]
        public void DeleteCustomerCommand_WrongUrl_Test()
        {
            DeleteCustomerCommand command = new DeleteCustomerCommand(new MySQLCustomerDAO(""));

            Request testRequest = new Request("DELETE", "/api/customer", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the CreateCustomerCommand checks the request method and Url correctly")]
        public void DeleteCustomerCommand_RightMethodAndUrl_Test()
        {
            DeleteCustomerCommand command = new DeleteCustomerCommand(new MySQLCustomerDAO(""));

            Request testRequest = new Request("DELETE", "/api/customer/5", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsTrue(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void FindCustomersCommand_WrongMethod_Test()
        {
            FindCustomersCommand command = new FindCustomersCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("q", "L");

            Request testRequest = new Request("DELETE", "/api/customer", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void FindCustomersCommand_WrongUrl_Test()
        {
            FindCustomersCommand command = new FindCustomersCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("q", "L");

            Request testRequest = new Request("GET", "/api/customer/8", new Version(1, 1), new Dictionary<string, string>(), args);

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void FindCustomersCommand_MissingGetParameter_Test()
        {
            FindCustomersCommand command = new FindCustomersCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/customer", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void FindCustomersCommand_WrongTypeGetParameter_Test()
        {
            FindCustomersCommand command = new FindCustomersCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("q", "L");
            args.Add("type", "customer");

            Request testRequest = new Request("GET", "/api/customer/", new Version(1, 1), new Dictionary<string, string>(), args);

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void GetCustomerCommand_WrongMethod_Test()
        {
            GetCustomerCommand command = new GetCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/customer/5", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void GetCustomerCommand_WrongUrl_Test()
        {
            GetCustomerCommand command = new GetCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/customer/a", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void GetCustomerCommand_TooManyUrlParts_Test()
        {
            GetCustomerCommand command = new GetCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/customer/5/3", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void UpdateCustomerCommand_WrongUrl_Test()
        {
            UpdateCustomerCommand command = new UpdateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("PUT", "/api/customer/abc", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void UpdateCustomerCommand_WrongMethod_Test()
        {
            UpdateCustomerCommand command = new UpdateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("DELETE", "/api/customer/6", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void UpdateCustomerCommand_EmptyBody_Test()
        {
            UpdateCustomerCommand command = new UpdateCustomerCommand(new MySQLCustomerDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("PUT", "/api/customer/5", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void CreateInvoiceCommand_WrongUrl_Test()
        {
            CreateInvoiceCommand command = new CreateInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/customer/", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void CreateInvoiceCommand_WrongMethod_Test()
        {
            CreateInvoiceCommand command = new CreateInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("PUT", "/api/invoice", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void CreateInvoiceCommand_EmptyBody_Test()
        {
            CreateInvoiceCommand command = new CreateInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/invoice/", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void DeleteInvoiceCommand_WrongUrl_Test()
        {
            DeleteInvoiceCommand command = new DeleteInvoiceCommand(new MySQLInvoiceDAO(""));

            Request testRequest = new Request("DELETE", "/api/customer/1", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void DeleteInvoiceCommand_WrongMethod_Test()
        {
            DeleteInvoiceCommand command = new DeleteInvoiceCommand(new MySQLInvoiceDAO(""));

            Request testRequest = new Request("POST", "/api/invoice/6", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }


        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void DeleteInvoiceCommand_InvalidId_Test()
        {
            DeleteInvoiceCommand command = new DeleteInvoiceCommand(new MySQLInvoiceDAO(""));

            Request testRequest = new Request("DELETE", "/api/invoice/asd", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void FindInvoicesCommand_WrongUrl_Test()
        {
            FindInvoicesCommand command = new FindInvoicesCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/invoic", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void FindInvoicesCommand_WrongMethod_Test()
        {
            FindInvoicesCommand command = new FindInvoicesCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("PUT", "/api/invoice/", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }


        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void FindInvoicesCommand_NoGetParameters_Test()
        {
            FindInvoicesCommand command = new FindInvoicesCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/invoice", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void GetInvoiceCommand_WrongUrl_Test()
        {
            GetInvoiceCommand command = new GetInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/invoice/", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        public void GetInvoiceCommand_WrongMethod_Test()
        {
            GetInvoiceCommand command = new GetInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("POST", "/api/invoice/6", new Version(1, 1), new Dictionary<string, string>());

            Assert.IsFalse(command.CanExecute(testRequest));
        }

        [TestMethod]
        [Description("Tests if the FindCustomersCommand recognizes a wrong request method")]
        [ExpectedException(typeof(BadRequestException))]
        public void GetInvoiceCommand_InvalidId_Test()
        {
            GetInvoiceCommand command = new GetInvoiceCommand(new MySQLInvoiceDAO(""), new JsonSerializerSettings());

            Request testRequest = new Request("GET", "/api/invoice/a", new Version(1, 1), new Dictionary<string, string>());

            command.Execute(testRequest);
        }
    }
}
